import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-question-checkbox',
  templateUrl: './question-checkbox.component.html',
  styleUrls: ['./question-checkbox.component.scss']
})
export class QuestionCheckboxComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
    this.copyQuestion();
    let options = this.question.options;
    this.key = options[options.length-1].key;
    if (!this.question.answer.selected) {
      this.question.answer.selected = [];
    }
  }

  setSelectedValue(checked: boolean, value: string) {
    let selected = this.question.answer.selected;
    let index: number = selected.indexOf(value);
    if (checked) {
      if (index < 0) {
        selected.push(value);
      }
    } else {
      if (index > -1) {
        selected.splice(index, 1);
      }
    }
  }



}
